/**
 * @desc: 格式化时间
 * @return: eg: '2018/04/09 21:31:00'
 * @param {Date对象} date 
 */
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

/**
 * @desc: 格式化日期
 * @return: eg: '2018/04/09 21:31:00'
 * @param {Date对象} date 
 */
const formatDate = date => {

  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}

//字符串转日期格式，strDate要转为日期格式的字符串 
const getDate = function getDate(strDate) {
  var st = strDate;
  var a = st.split(" "); //这个根据你的字符串决定，如果中间为T则改T
  var b = a[0].split("-");
  var c = a[1].split(":");
  var date = new Date(b[0], b[1], b[2], c[0], c[1], c[2]);
  return date;
}
/**
 * @desc: 格式化数字
 * @return: n > 10 [eg: 12] => 12 | n < 10 [eg: 3] => '03'
 * @param {*} n 
 */
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const getDateYYYYMMDDHHMISS = function () {
  let date = new Date();
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hour = date.getHours();
  let minute = date.getMinutes();
  let second = date.getSeconds();
  if (month < 10) {
    month = '0' + month;
  }
  if (day < 10) {
    day = '0' + day;
  }
  if (hour < 10) {
    hour = '0' + hour;
  }
  if (minute < 10) {
    minute = '0' + minute;
  }
  if (second < 10) {
    second = '0' + second;
  }
  return year + "" + month + "" + day + "" + hour + "" + minute + "" + second;
}

const addYear = function (_date, _year) {
  let year = _date.getFullYear() + _year; //获取当前年份
  let mon = _date.getMonth(); //获取当前月份
  let da = _date.getDate(); //获取当前日
  let h = _date.getHours(); //获取小时
  let m = _date.getMinutes(); //获取分钟
  let s = _date.getSeconds(); //获取秒
  let newDate = new Date(year, mon, da, h, m, s);
  return newDate;
}

const addMonth = function (_date, _month) {
  let year = _date.getFullYear(); //获取当前年份
  let mon = _date.getMonth() + _month; //获取当前月份
  let da = _date.getDate(); //获取当前日
  let h = _date.getHours(); //获取小时
  let m = _date.getMinutes(); //获取分钟
  let s = _date.getSeconds(); //获取秒
  let newDate = new Date(year, mon, da, h, m, s);
  return newDate;
}

const addDay = function (_date, _day) {
  let year = _date.getFullYear(); //获取当前年份
  let mon = _date.getMonth(); //获取当前月份
  let da = _date.getDate() + _day; //获取当前日
  let h = _date.getHours(); //获取小时
  let m = _date.getMinutes(); //获取分钟
  let s = _date.getSeconds(); //获取秒
  let newDate = new Date(year, mon, da, h, m, s);
  return newDate;
}

const addHour = function (_date, _hour) {
  let year = _date.getFullYear(); //获取当前年份
  let mon = _date.getMonth(); //获取当前月份
  let da = _date.getDate(); //获取当前日
  let h = _date.getHours() + _hour; //获取小时
  let m = _date.getMinutes(); //获取分钟
  let s = _date.getSeconds(); //获取秒
  let newDate = new Date(year, mon, da, h, m, s);
  return newDate;
}


const addMinutes = function (_date, _minute) {
  let year = _date.getFullYear(); //获取当前年份
  let mon = _date.getMonth(); //获取当前月份
  let da = _date.getDate(); //获取当前日
  let h = _date.getHours(); //获取小时
  let m = _date.getMinutes() + _minute; //获取分钟
  let s = _date.getSeconds(); //获取秒
  let newDate = new Date(year, mon, da, h, m, s);
  return newDate;
}

const addSeconds = function (_date, _second) {
  let year = _date.getFullYear(); //获取当前年份
  let mon = _date.getMonth(); //获取当前月份
  let da = _date.getDate(); //获取当前日
  let h = _date.getHours(); //获取小时
  let m = _date.getMinutes(); //获取分钟
  let s = _date.getSeconds() + _second; //获取秒
  let newDate = new Date(year, mon, da, h, m, s);
  return newDate;
}

// 富文本工具类
const formatRichText = (html) => {
  let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
    match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
    match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
    match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
    return match;
  });
  newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
    match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
    return match;
  });
  newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');


  //判断img是否存在
  if (newContent.indexOf('img') != -1) {
    var result = newContent.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
      // console.log(capture);
      return '<img src=' + config().api + capture + ' style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"/>';
    });
    return result;
  }


  return newContent;
}

function js_date_time(value) {
  console.log(value)
  if (!value) return '';
  var now = new Date(value);
  console.log("time:" + JSON.stringify(now));
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  if (month < 10) {
    month = '0' + month
  }
  var date = now.getDate();
  if (date < 10) {
    date = '0' + date
  }
  return year + "-" + month + "-" + date
}
/**
 * @desc: js 获取当前日后_未来_30天每一天日期
 * @return: eg: '2020/07/08 '
 * @param {}   // ["11-06", "11-07", "11-08", "11-09", "11-10", "11-11", "11-12", "11-13", "11-14", "11-15", "11-16", "11-17", "11-18", "11-19", "11-20", "11-21", "11-22", "11-23", "11-24", "11-25", "11-26", "11-27", "11-28", "11-29", "11-30", "12-01", "12-02", "12-03", "12-04", "12-05", "12-06"]
 */
function getDay30After() {
  var dateList = [];
  let startDate = new Date();
  let endDate = new Date();
  endDate.setDate(startDate.getDate() + 30);
  while ((endDate.getTime() - startDate.getTime()) >= 0) {
    let year = startDate.getFullYear().toString()
    let month = (startDate.getMonth() + 1).toString().length === 1 ? "0" + (startDate.getMonth() + 1).toString() : (
      startDate.getMonth() + 1);
    let day = startDate.getDate().toString().length === 1 ? "0" + startDate.getDate() : startDate.getDate();
    dateList.push(year+'-'+month + "-" + day);
    startDate.setDate(startDate.getDate() + 1);
  }
  return dateList
}
const config = function () {
  return {
    api: `http://192.168.0.68:8082/manage`, //本地
    // api: `https://hj.lcxwkj.com/meeting`, //在线
    // api: `http://106.13.123.192:8081`, //在线
    // api: 'http://127.0.0.1:8086',
    // 鉴于该小程序有分前端用户和后端用户   区分两个token  一般项目只用前端token即可
    // token: wx.getStorageSync('token'),
    // bToken: wx.getStorageSync('bToken'),
    // app_id: api.appInfo.appId,
    // transaction_id: wxuuid(),
    // req_time: getDateYYYYMMDDHHMISS(),
    // sign:'1234567',
    // user_id:'-1',
    // cookie: '_java110_token_=' + wx.getStorageSync('token')
    key: 'J3LBZ-ERWCU-RX4VX-2NUQQ-NOBY5-2VFGJ', //腾讯地图
  }
}
module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  getDateYYYYMMDDHHMISS: getDateYYYYMMDDHHMISS,
  getDate: getDate,
  addYear: addYear,
  addMonth: addMonth,
  addDay: addDay,
  addHour: addHour,
  addMinutes: addMinutes,
  addSeconds: addSeconds,
  js_date_time: js_date_time,
  formatRichText: formatRichText,
  getDay30After: getDay30After,
  config: config
}